// pages/category/category.js
const jokesData = require('../../data/jokes');
const categoriesData = require('../../data/categories');
Page({
  data: {
    categories: [],
    selectedCategory: null,
    jokes: []
  },
  onLoad: function () {
    this.setData({
      categories: categoriesData
    });
  },
  onCategoryTap: function (e) {
    let categoryId = e.currentTarget.dataset.id;
    let category = categoriesData.find(c => c.id === categoryId);
    if (this.data.selectedCategory !== categoryId) {
      let categoryJokes = jokesData.filter(joke => joke.category === category.name);
      this.setData({
        selectedCategory: categoryId,
        jokes: categoryJokes
      });
    } else {
      this.setData({
        selectedCategory: null,
        jokes: []
      });
    }
  },
  onJokeTap: function (e) {
    let jokeId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/joke-detail/joke-detail?id=' + jokeId
    });
  }
})
